package com.wanmait.zaisousuo.aop;


import com.wanmait.zaisousuo.pojo.Admin;
import com.wanmait.zaisousuo.service.AdminLogService;
import com.wanmait.zaisousuo.util.SysContext;
import org.apache.solr.client.solrj.cloud.autoscaling.Policy;
import org.apache.zookeeper.server.SessionTracker;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class LogAdminAspect {
    @Resource
    private AdminLogService adminLogService;

    private Integer getAdminId(){
        HttpServletRequest request = SysContext.getRequest();
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        return admin.getId();
    }




    //禁用、启用管理员
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AdminService.changeDisabled(..))")
    public void stopOrStartAdmin()
    {}

    //添加管理员
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AdminService.addAdmin(..))")
    public void addAdmin()
    {}

    //管理员登录
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AdminService.login(..))")
    public void login()
    {}

    //文章禁用/启用
    @Pointcut("execution( * com.wanmait.zaisousuo.service.ArticleService.changeDisabled(..))")
    public void changeDisabled()
    {}

    //批量删除提问
    @Pointcut("execution( * com.wanmait.zaisousuo.service.QuestionService.delete(..))")
    public void deleteQuestions()
    {}

    //批量删除回答
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AnswerService.delete(String))")
    public void deleteAnswers()
    {}

    //批量恢复提问
    @Pointcut("execution( * com.wanmait.zaisousuo.service.QuestionService.recover(..))")
    public void recoverQuestions()
    {}

    //批量恢复回答
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AnswerService.recover(..))")
    public void recoverAnswers()
    {}

    //单个提问的禁用/恢复：id
    @Pointcut("execution( * com.wanmait.zaisousuo.service.QuestionService.updateDisabled(..))")
    public void updateDisabledQuestions()
    {}

    //单个回答的禁用/恢复：id
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AnswerService.updateDisabled(..))")
    public void updateDisabledAnswers()
    {}

    //永久删除问题
    @Pointcut("execution( * com.wanmait.zaisousuo.service.QuestionService.realDelete(..))")
    public void realDeleteQuestion()
    {}

    //永久删除回答
    @Pointcut("execution( * com.wanmait.zaisousuo.service.AnswerService.delete(Integer))")
    public void realDeleteAnswers(){}

    //删除一个垃圾处理方式
    @Pointcut("execution( * com.wanmait.zaisousuo.service.GarbageService.updateReduceCountById(..))")
    public void deleteSolution(){}

    //假删除垃圾
    @Pointcut("execution( * com.wanmait.zaisousuo.service.GarbageService.delete(Integer))")
    public void deleteGarbage(){}

    //更改审核状态
    @Pointcut("execution( * com.wanmait.zaisousuo.service.GarbageService.changeState(..))")
    public void changeStates(){}

    //修改垃圾
    @Pointcut("execution( * com.wanmait.zaisousuo.service.GarbageService.update(..))")
    public void updateGarbage(){}

    //更改垃圾审核状态
    @Pointcut("execution( * com.wanmait.zaisousuo.service.TextureService.changeTextureState(..))")
    public void updateTexture(){}

    //VIP审核通过和不通过
    @Pointcut("execution( * com.wanmait.zaisousuo.service.UserinfoService.changeVipStatus(..))")
    public void checkVIP(){}

    //企业审核通过和不通过
    @Pointcut("execution( * com.wanmait.zaisousuo.service.UserinfoService.changeCompanyStatus(..))")
    public void checkCompany(){}

    //删除文章
    @Pointcut("execution( * com.wanmait.zaisousuo.service.ArticleService.deleteArticleAndRelative(..))")
    public void deleteArticleAndRelative(){}

    //封号
    @Pointcut("execution( * com.wanmait.zaisousuo.service.UserinfoService.banUser(..))")
    public void banUser(){}

    //解封
    @Pointcut("execution( * com.wanmait.zaisousuo.service.UserinfoService.recoverUser(..))")
    public void recoverUser(){}

    //后台启用/禁用数据
    @Pointcut("execution( * com.wanmait.zaisousuo.service.CompanyDataService.changeStateById(..))")
    public void changeStateById(){}








    @AfterReturning(value = "stopOrStartAdmin()",returning = "returnObj")
    public void afterReturningstopOrStartAdmin(JoinPoint joinPoint,Object returnObj) {
        String s[] = returnObj.toString().split("&split&");
        if(s[0].equals("true"))s[0]="禁止";
        if(s[0].equals("false"))s[0]="启用";

        adminLogService.insertOneLog(getAdminId(),"超级管理员 "+s[0]+" 了管理员 "+s[1]);
    }

    @AfterReturning(value = "addAdmin()",returning = "returnObj")
    public void afterReturningaddAdmin(JoinPoint joinPoint,Object returnObj){
        Integer s = (Integer)returnObj;
        if(s==0) {
            adminLogService.insertOneLog(getAdminId(),"添加了一个管理员");
        }
    }



    @AfterReturning(value = "login()",returning = "returnObj")
    public void afterLogin(JoinPoint joinPoint,Object returnObj){
        Admin admin = (Admin)returnObj;
        try {
            adminLogService.insertOneLog(admin.getId(),"管理员"+admin.getAdminName()+"登陆成功");
        }catch (NullPointerException e){

        }
    }


    @AfterReturning(value = "changeDisabled()",returning = "returnObj")
    public void afterChangeDisabled(JoinPoint joinPoint,Object returnObj){
        String s = "";
        Boolean f = (Boolean)returnObj;
        if(f==true)s="禁止";
        if(f==false)s="启用";
        adminLogService.insertOneLog(getAdminId(),s+"了一篇文章");
    }

    @After("deleteQuestions()")
    public void afterDeleteQuestions(JoinPoint joinPoint)
    {
        adminLogService.insertOneLog(getAdminId(),"禁止了多个问题");
    }

    @After("deleteAnswers()")
    public void afterDeleteAnswers(JoinPoint joinPoint)
    {
        adminLogService.insertOneLog(getAdminId(),"禁止了多个回答");
    }

    @After("recoverQuestions()")
    public void afterRecoverQuestions(JoinPoint joinPoint)
    {
        adminLogService.insertOneLog(getAdminId(),"启用了多个问题");
    }

    @After("recoverAnswers()")
    public void afterRecoverAnswers(JoinPoint joinPoint)
    {
        adminLogService.insertOneLog(getAdminId(),"启用了多个回答");
    }

    @AfterReturning(value = "updateDisabledQuestions()",returning = "returnObj")
    public void afterReturningDisabledQuestions(JoinPoint joinPoint,Object returnObj){
        adminLogService.insertOneLog(getAdminId(),"禁止/启用了问题，问题id："+returnObj.toString());
    }

    @AfterReturning(value = "updateDisabledAnswers()",returning = "returnObj")
    public void afterReturningDisabledAnswers(JoinPoint joinPoint,Object returnObj){
        adminLogService.insertOneLog(getAdminId(),"禁止/启用了回答，回答id："+returnObj.toString());
    }

    @After("realDeleteQuestion()")
    public void afterRealDeleteQuestions(JoinPoint joinPoint){
        adminLogService.insertOneLog(getAdminId(),"永久删除了1个问题");
    }

    @After("realDeleteAnswers()")
    public void afterRealDeleteAnswers(JoinPoint joinPoint){
        adminLogService.insertOneLog(getAdminId(),"永久删除1个回答");
    }

    @AfterReturning(value = "deleteSolution()",returning = "returnObj")
    public void afterReturningDeleteSolution(JoinPoint joinPoint,Object returnObj){
        Integer s = (Integer)returnObj;
        adminLogService.insertOneLog(getAdminId(),"删除了一条垃圾处理方式，垃圾id："+s);
    }

    @After("deleteGarbage()")
    public void afterDeleteGarbage(JoinPoint joinPoint){
        adminLogService.insertOneLog(getAdminId(),"删除了一个垃圾");
    }

    @AfterReturning(value = "changeStates()",returning = "returnObj")
    public void afterReturningChangeStates(JoinPoint joinPoint,Object returnObj){
        Boolean flag = (Boolean)returnObj;
        if(flag==true)adminLogService.insertOneLog(getAdminId(),"审核通过了一个垃圾");
        if(flag==false)adminLogService.insertOneLog(getAdminId(),"取消了一个垃圾");
    }

    @After("updateGarbage()")
    public void afterUpdateGarbage(JoinPoint joinPoint){
        adminLogService.insertOneLog(getAdminId(),"修改垃圾信息");
    }

    @AfterReturning(value = "updateTexture()",returning = "returnObj")
    public void afterReturningChangeTextureStates(JoinPoint joinPoint,Object returnObj){
        Boolean flag = (Boolean)returnObj;
        if(flag==true)adminLogService.insertOneLog(getAdminId(),"启用了一个垃圾材质");
        if(flag==false)adminLogService.insertOneLog(getAdminId(),"禁止了一个垃圾材质");
    }

    @AfterReturning(value = "checkVIP()",returning = "returnObj")
    public void afterReturningCheckVIP(JoinPoint joinPoint,Object returnObj){
        String s[] = returnObj.toString().split("&split&");
        if(s[1].equals("true"))s[1]="审核通过 了";
        if(s[1].equals("false"))s[1]="驳回 了";
        adminLogService.insertOneLog(getAdminId(),s[1]+"一条VIP申请，用户id："+s[0]);
    }

    @After("deleteArticleAndRelative()")
    public void afterdeleteArticleAndRelative(JoinPoint joinPoint){
        adminLogService.insertOneLog(getAdminId(),"永久删除了一篇文章");
    }

    @AfterReturning(value = "banUser()",returning = "returnObj")
    public void afterReturningBan(JoinPoint joinPoint,Object returnObj){
        Integer a = (Integer)returnObj;
        adminLogService.insertOneLog(getAdminId(),"账号封号操作，用户id："+a);
    }

    @AfterReturning(value = "recoverUser()",returning = "returnObj")
    public void afterReturningRecover(JoinPoint joinPoint,Object returnObj){
        Integer a = (Integer)returnObj;
        adminLogService.insertOneLog(getAdminId(),"账号解封操作，用户id："+a);
    }

    @AfterReturning(value = "checkCompany()",returning = "returnObj")
    public void afterReturningCompanyStates(JoinPoint joinPoint,Object returnObj){
        String s[] = returnObj.toString().split("&split&");
        if(s[1].equals("true"))s[1]="审核通过 了";
        if(s[1].equals("false"))s[1]="驳回 了";
        adminLogService.insertOneLog(getAdminId(),s[1]+"一条企业用户认证申请，用户id："+s[0]);
    }

    @AfterReturning(value = "changeStateById()",returning = "returnObj")
    public void afterReturningChangeStateById(JoinPoint joinPoint,Object returnObj){
        String s[] = returnObj.toString().split("&split&");
        if(s[1].equals("true"))s[1]="启用了";
        if(s[1].equals("false"))s[1]="删除了";
        adminLogService.insertOneLog(getAdminId(),s[1]+"一条统计数据，数据id："+s[0]);
    }


}
